package demo.view;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.sql.SQLException;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import demo.dao.ReaderDao;
import demo.entity.Reader;
import demo.util.MD5Util;

public class UserEnter extends JFrame implements ActionListener{
	
	private JFrame frame = new JFrame("图书馆管理系统 ");
	private JPanel ReaderEnterPane;
	public static String name,password;
	public static String number;
	private ImageIcon background;
	private JTextField ReaderNametextField;// 用于输入用户名
	private JPasswordField ReaderPasswordtextField;// 用于输入密码
	private JLabel lblStatus;// 用于显示登录失败时的错误信息
	Reader loginReader;// 用户保存登录用户的信息
	


	public UserEnter() 
	{
		 background = new ImageIcon(this.getClass().getResource("background.jpg"));
		 JLabel label = new JLabel(background);
		 label.setBounds(0, 0, background.getIconWidth(),background.getIconHeight());
		
		 ReaderEnterPane = (JPanel) frame.getContentPane(); 
		 //getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
		 ReaderEnterPane.setOpaque(false);
		 ReaderEnterPane.setLayout(null);
		 frame.getLayeredPane().setLayout(null);
		 
		 frame.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE)); 

		 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		 frame.setBounds(500, 200, 450, 300);
		 frame.setResizable(false);
		 frame.setVisible(true);

		
		JLabel ReaderNameLabel = new JLabel("学号:");
		ReaderEnterPane.add(ReaderNameLabel);
		ReaderNameLabel.setBounds(90, 37, 61, 37);
		ReaderNameLabel.setFont(new Font("",Font.BOLD,20));
		ReaderNameLabel.setForeground(Color.white);
		
		
		JLabel ReaderPasswordLabel = new JLabel("密码:");
		ReaderEnterPane.add(ReaderPasswordLabel);
		ReaderPasswordLabel.setBounds(90, 121, 61, 24);
		ReaderPasswordLabel.setFont(new Font("",Font.BOLD,20));
		ReaderPasswordLabel.setForeground(Color.white);
		
		
		JTextField ReaderNametextField=new JTextField();
		ReaderEnterPane.add(ReaderNametextField);
		ReaderNametextField.setBounds(156, 39, 178, 37);
		ReaderNametextField.setColumns(10);
		
		JPasswordField ReaderPasswordtextField;
		ReaderPasswordtextField = new JPasswordField();
		ReaderEnterPane.add(ReaderPasswordtextField);
		ReaderPasswordtextField.setBounds(156, 117, 178, 37);
		ReaderPasswordtextField.setColumns(10);
		
		JButton returnButton = new JButton("↩");
		ReaderEnterPane.add(returnButton);
		returnButton.setBounds(10, 5, 61,25);
		returnButton.setFont(new Font("↩",Font.BOLD,15));
		returnButton.addActionListener(this);
		
		
		JButton RegisterButton = new JButton("注册");
		ReaderEnterPane.add(RegisterButton);
		RegisterButton.setBounds(97, 187, 75, 37);
		RegisterButton.setFont(new Font("注册",Font.BOLD,15));
		RegisterButton.addActionListener(this);
		
		
		JButton LandButton = new JButton("登录");
		ReaderEnterPane.add(LandButton);
		LandButton.setBounds(259, 187, 75, 37);
		LandButton.setFont(new Font("",Font.BOLD,15));
		//LandButton.setActionCommand("Login");
		LandButton.addActionListener(this);
		
		
		JLabel ForgetLabel = new JLabel("忘记密码？");
		ReaderEnterPane.add(ForgetLabel);
		ForgetLabel.setBounds(358, 123, 66, 24);
		ForgetLabel.setForeground(Color.blue);
				
		
		frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);;
		
		ForgetLabel.addMouseListener(new MouseListener() {
		
			@Override
			public void mouseClicked(MouseEvent arg0) {
				System.out.println("+1s");
			}
			@SuppressWarnings("deprecation")
			@Override
			public void mouseEntered(MouseEvent arg0) {
				setCursor(HAND_CURSOR);
			}
			@SuppressWarnings("deprecation")
			@Override
			public void mouseExited(MouseEvent arg0) {
				setCursor(DEFAULT_CURSOR);
			}
			@Override
			public void mousePressed(MouseEvent arg0) {}
			@Override
			public void mouseReleased(MouseEvent arg0) {}				
			});
		
		returnButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub	
				frame.dispose();
				new Welcome();
			}			
		});
		
		RegisterButton.addActionListener(new ActionListener() {
					@Override
					public void actionPerformed(ActionEvent e) {
						// TODO Auto-generated method stub					
								new ReaderRegister();
					}			
				});
		
		LandButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getSource()==LandButton) {
					// 调用authenticate方法验证用户身份
					Reader reader = authenticate(ReaderNametextField.getText().trim(), new String(ReaderPasswordtextField.getPassword()).trim());
					if (reader != null) {// 如果用户身份有效
						loginReader = reader;// 将登录成功的用户赋值给成员变量loginUser,以供login方法返回给调用者
						frame.dispose(); 
						//new ReaderFrame();
					} else
						JOptionPane.showMessageDialog(null,"账号或密码错误！","账号或密码错误", JOptionPane.WARNING_MESSAGE);
					
				}
				
			}
			
				public Reader authenticate(String id, String password) {
	  	        String passwordMD5 = password;// 将输入的密码串转换成MD5密文
		        ReaderDao readerDao = new ReaderDao();
		        Reader reader = readerDao.findById(id);// 根据输入的用户id，在users表中查找用户
		        // 如果找到用户，并且数据库中保存的用户密码的MD5密文与输入的密码串的MD5密文相等，表明身份验证正确
		        if (reader != null && reader.getPassword().equals(passwordMD5)) {
			        return reader;
		    }
		        else
			        return null;
	        }
	 
	           public Reader login() {
		           //this.setVisible(true);
		       return loginReader;
	         }
		});
	}
	

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO 自动生成的方法存根
		
	}
	
     }
